package Limit.Skill.Jewelry;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.utils.SQLUtil;

public class Jewelry_Table {
	
	private static Jewelry_Table _instance;
	
	private final HashMap<Integer, Jewelry_Load> _LimitSkillInfo = new HashMap<Integer, Jewelry_Load>();
	
	public static Jewelry_Table getInstance() {
		if (_instance == null) {
			_instance = new Jewelry_Table();
		}
		return _instance;
	}
	
	public int loadCount(int char_id)
	{
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		int i = 0;
		try 
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM limitskill_jewelry WHERE Char_ID = ?");
			pstm.setInt(1, char_id);
			rs = pstm.executeQuery();
			
			while (rs.next())
			{
				i++;
			}
		}
		
		catch (SQLException e) 
		{
			System.out.print(e.getMessage());
		}
		finally 
		{
			SQLUtil.close(rs, pstm, con);
		}	
		return i;
	}
	
	public void load(int char_id){
		int count = loadCount(char_id);
		int[][] skillInfo = new int[count][2] ;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		
		int i;
		for (i = 0; i < count; i++)
			skillInfo[i][0] = 0;
		i = 0;
		try 
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM limitskill_jewelry WHERE Char_ID = ? Order By Skill_ID ASC");
			pstm.setInt(1, char_id);
			rs = pstm.executeQuery();
			
			while (rs.next())
			{
				skillInfo[i][0] = rs.getInt("Skill_ID");
				skillInfo[i][1] = rs.getInt("Level");
				i++;
			}
		}
		catch (SQLException e) 
		{
			System.out.print(e.getMessage());
		}
		finally 
		{
			SQLUtil.close(rs, pstm, con);
		}
		
		Jewelry_Load info = new Jewelry_Load();
		Integer id = char_id;
		info.setSkillInfo(skillInfo);
		info.setCount(count);

		_LimitSkillInfo.put(id, info);
	}
	
	public Jewelry_Load getTemplateID(int ID) {
		if (_LimitSkillInfo.containsKey(ID)) {
			return _LimitSkillInfo.get(ID);
		}
		return null;
	}
}
